/* eslint-disable max-params */
import request from '@/utils/request';
const basePath = '/appsys/common/select';

const httpRequest = (url, parameter, method) =>
  request({
    url,
    data: parameter,
    method
  });
// 人员，部门默认选中
const getInitDeptInfo = (deptids = []) =>
  request({
    url: `${basePath}/getSelectedDeptInfo/v1`,
    data: deptids,
    method: 'POST'
  });
// 获取当前组织
const searchOrgName = async function (id = '', viewScope = '', defaultOrgId = '') {
  const result = await request({
    url: `${basePath}/searchOrgName/v1`,
    data: {
      id,
      defaultOrgId,
      viewScope
    },
    method: 'POST'
  });
  return new Promise((resolve) => {
    resolve(result);
  });
};
// 获取组织列表
const selectOrgName = async function (viewScope = '', defaultOrgId = '') {
  const result = await request({
    url: `${basePath}/selectOrgName/v1`,
    data: {
      viewScope,
      defaultOrgId
    },
    method: 'POST'
  });
  return new Promise((resolve) => {
    resolve(result);
  });
};
const getInitOrgInfo = async (orgids = []) => {
  const result = await request({
    url: '/appsys/common/select/getSelectedOrgInfo/v1',
    data: orgids,
    method: 'post'
  });
  return new Promise((resolve) => {
    if (result.success) {
      resolve(result);
    } else {
      resolve(null);
    }
  });
};
const getDeptAndPosition = async (ids) => {
  const result = await request({
    url: '/appsys/user/getDeptAndPositionListBySysUserId/v1',
    data: ids,
    method: 'post'
  });
  return new Promise((resolve) => {
    if (result.success) {
      resolve(result.data[0]);
    } else {
      resolve(null);
    }
  });
};

const getRoles = async (id) => {
  const result = await request({
    url: '/appsys/role/RoleRest/getRolesByUserId/v1',
    data: { userId: id, appId: 1 },
    method: 'post'
  });
  return new Promise((resolve) => {
    if (result.success) {
      resolve(result.result);
    } else {
      resolve(null);
    }
  });
};
const getOrg = (defaultOrgId = '', id = '', viewScope = '') =>
  request({
    url: `${basePath}/searchOrgName/v1`,
    data: {
      id,
      defaultOrgId,
      viewScope
    },
    method: 'post'
  });
export { getInitDeptInfo, getInitOrgInfo, searchOrgName, selectOrgName, getDeptAndPosition, getRoles, getOrg, httpRequest };

/**
 * 分页获取角色/岗位/群组维度的用户数据
 * @param queryParam 分页查询参数
 * @returns 表格数据
 */
export function listCommonSelectUserByPage(queryParam) {
  return request.post(`${basePath}/findUserListByPage/v1`, queryParam);
}

export function getDeptByIds(deptids = []) {
  return request.post(`${basePath}/getDeptByIds/v1`, deptids);
}
